package com.atbun.ucenterservice.controller;


import com.atbun.commonutils.JwtUtils;
import com.atbun.commonutils.R;
import com.atbun.commonutils.vo.UcenterMemberVo;
import com.atbun.ucenterservice.entity.UcenterMember;
import com.atbun.ucenterservice.entity.vo.RegisterVo;
import com.atbun.ucenterservice.service.UcenterMemberService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 登录注册
 * </p>
 *
 * @author testjava
 * @since 2020-05-15
 */
@RestController
@RequestMapping("/ucenterservice/ucentermember")
public class UcenterMemberController {
    @Autowired
    private UcenterMemberService ucenterMemberService;

    @ApiOperation(value = "会员登录")
    @PostMapping("login")
    public R login(@RequestBody UcenterMember ucenterMember) {
//        token使用JWT令牌生成
        String token = ucenterMemberService.login(ucenterMember);
        return R.ok().data("token", token);
    }

    @ApiOperation(value = "会员注册")
    @PostMapping("register")
    public R register(@RequestBody RegisterVo registerVo){
        boolean save = ucenterMemberService.register(registerVo);
        if (save){
            return R.ok();
        }else{
            return R.error();
        }
    }

    @ApiOperation(value = "根据token获取登录信息")
    @GetMapping("getLoginInfo")
    public R getLoginInfo(HttpServletRequest request){
//        调用jwt工具，根据request对象获取请求头，返回用户id
        String memberIdByJwtToken = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember member = this.ucenterMemberService.getById(memberIdByJwtToken);
        return R.ok().data("userInfo",member);
    }

    @ApiOperation(value = "根据用户id获取用户信息")
    @GetMapping("getUcenterInfo/{id}")
    public UcenterMemberVo getUcenterInfo(@PathVariable String id){
        UcenterMember ucenterMember = this.ucenterMemberService.getById(id);

        UcenterMemberVo memeber = new UcenterMemberVo();
        BeanUtils.copyProperties(ucenterMember,memeber);
        return memeber;
    }

    @ApiOperation(value = "统计每一天的注册人数")
    @GetMapping("countRegister/{day}")
    public R countRegister(@PathVariable String day){
        Integer count = this.ucenterMemberService.countRegister(day);
        return R.ok().data("countRegister",count);
    }

}

